import { IRouteObject, UserLevel } from "@/router/baseRouter.tsx";

export const routeFilter = (
  routes: IRouteObject[],
  role: number = UserLevel["ADMIN"],
): IRouteObject[] => {
  const getRoutes = (
    routes: IRouteObject[],
    role: number = UserLevel["ADMIN"],
  ) => {
    const newRoutes = [] as IRouteObject[];
    for (const route of routes) {
      if (route.meta?.role) {
        // role 不是数组，要求用户等级大于等于role
        if (role < route.meta.role) continue;
      }

      const item = { ...route };
      // 如果路由有子路由，则递归过滤子路由
      if (route.children) {
        item.children = routeFilter(route.children, role);
      }
      newRoutes.push(item);
    }

    return newRoutes;
  };
  const newRoutes = getRoutes(routes, role);
  return newRoutes.filter(
    (route) => !route.children || route.children?.length != 0,
  );
};
